package com.demo.controller.system;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.demo.dao.Result;
import com.demo.mapper.system.RoleMapper;
import com.demo.pojo.DTO.DTORoleMenu;
import com.demo.pojo.system.Role;
import com.demo.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Validated
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMapper roleMapper;

    @PostMapping
    public Result addRole(@RequestBody Role role) {
        boolean save = roleService.save(role);
        return Result.success(save);
    }

    @DeleteMapping
    public Result deleteRole(@RequestParam int id) {
        roleService.removeById(id);
        return Result.success();
    }
    // 批量删除
    @DeleteMapping(value = "/delList")
    public Result deleteRoleList(@RequestParam List<Integer> id) {
        roleService.removeByIds(id);
        return Result.success();
    }

    @PutMapping
    public Result updateRole(@RequestBody Role role) {
        roleService.updateById(role);
        return Result.success();
    }

    @GetMapping
    public Result getRoles(int pageNum, int pageSize,@RequestParam(required = false ,defaultValue = "") String roleName ) {
        Page<Role> page = roleService.lambdaQuery().like(Role::getRoleName,roleName).page(Page.of(pageNum, pageSize));
        return Result.success(page);
    }

    // 分配角色权限，先去调菜单表的树状图，再调一遍这个传入roleId获取该角色已有的权限,前端记得排除isDelete=0的（你问我为啥不排除？因为我下面需要用到没被排除的）
    @GetMapping(value = "/getMenuByRoleId")
    public Result getMenuByRoleId(@RequestParam int roleId) {
        return Result.success(roleMapper.selectRoleMenuById(roleId));
    }
    @PutMapping(value = "/putRoleMenu")
    public Result updateRoleMenu(@RequestParam Integer roleId,@RequestBody List<Integer> menuIds) {
        //删除全部这个角色的menu，然后新增这个角色新写入的menuId
        System.out.println(menuIds);
        // 软删除，设置isDeleted=0
        roleMapper.delAllMenuByRoleId(roleId,0);
        // 查询已有的，被软删除的，又需要设为1的,设为1，并找出没有的，放到下面去新增
        List<DTORoleMenu> roleMenu =  roleMapper.selectRoleMenuById(roleId);
        List<Integer> needBy3 = new ArrayList<>();
        List<Integer> needAddRoleMenu = menuIds.stream().filter(m1->{  // 需要新增的
            // 比较menuId和传入的id，找出有的要改的
               List<Integer> setDelIs1 =  roleMenu.stream().filter(m2->{return m2.getMenuId().equals(m1);}).map(m2->{
                   needBy3.add(m2.getId());
               return m2.getId(); }).collect(Collectors.toList());
            // 传入的id如果有，则比较结果应该有一个相等，如果没有一个相等的说明没有，说明比较找没有的
              Boolean i = roleMenu.stream().filter(m2->{return m2.getMenuId().equals(m1);}).collect(Collectors.toList()).size()==0;
            return i;
        }).collect(Collectors.toList());

        // 传入权限（菜单）id新增
        if (needAddRoleMenu.size()>0) {roleMapper.insertRoleMenu(roleId,needAddRoleMenu);}

        // 更加id修改
        if (needBy3.size()>0) {roleMapper.setDelIs(needBy3,1);}

        return Result.success();
    }
}
